home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / COMMUNIC / 0717.ZIP / MSBPCT.C < prev    next >
C/C++ Source or Header  |  1987-05-24  |  5KB  |  182 lines

  1. /*
  2.  * MSBPCT.C
  3.  *
  4.  * Howie Kaye -- Columbia University 3/11/86
  5.  *
  6.  * sibling program to MSBMKB.C.  It is used to unpack BOO files, used for
  7.  * encoding binary files into text, and back.  This program does the decoding.
  8.  * It is meant to replace the program "MSBPCT.BAS", and runs approximately
  9.  * 200 times faster.
  10.  *
  11.  * For documentation on BOO file format, see MSBMKB.C
  12.  * This program runs, as is, under Microsoft C on MSDOS, and under UNIX(4.2).
  13.  *
  14.  * Modification history:
  15.  *
  16.  * 3/23/86 - Davide P. Cervone -- University of Rochester
  17.  *   added AMIGA and VAX11C support
  18.  *
  19.  * 3/24/86 - Martin Knoblauch -- TH-Darmstadt              (MK001)
  20.  *   test if 1. line of inputfile is delimited by "\r\n" instead of "\n"
  21.  *
  22.  * 5/5/86 - John Matthews, U of Delaware.
  23.  *   Explicitly close the files.
  24.  *
  25.  * 5/8/86 - L. John Junod, DTNSRDC.
  26.  *   Adapt for Computer Innovations MS-DOS CI-86 Compiler
  27.  *   Improve too many args error message
  28.  */
  29.  
  30. #define MSDOS
  31. #define CI86
  32.  
  33. #include <stdio.h>
  34.  
  35. #ifdef AMIGA
  36. #include <fcntl.h>
  37. #else
  38. #endif
  39.  
  40. #ifdef MSDOS 
  41. #ifndef CI86
  42. #include <fcntl.h>
  43. #endif
  44. #else
  45. #ifdef vax11c
  46. #include <file.h>
  47. #else
  48. #include <sys/file.h>
  49. #endif
  50. #endif
  51.  
  52. #define fixchr(x) ((x) -'0')
  53. #define NULLCHR fixchr('~')
  54.  
  55. yes_or_no_p(arg) char *arg; {
  56.   int c,x;
  57.   while (1) {
  58.     printf("%s",arg);
  59.     c = getchar();
  60.     if (c == '\n') continue;
  61.     while ((x = getchar()) != '\n')
  62.       if (x == EOF) return(0);
  63.     if ((c == 'Y') || (c == 'y')) return(1);
  64.     if ((c == 'N') || (c == 'n') || (c == EOF)) return(0);
  65.     printf("Please answer 'Y' or 'N'\n");
  66.   }
  67. }
  68.  
  69. main(argc,argv) char **argv; {
  70. #ifdef AMIGA
  71.   char *infile = "CKIKER.BOO";          /* input file name, with default */
  72. #else
  73.   char *infile = "MSKERMIT.BOO";        /* input file name, with default */
  74. #endif
  75.   char outfile[100];                    /* output file name */
  76.   FILE *ifp, *ofp;                      /* i/o files */
  77.   char inline[100],outline[200];
  78.   int f;
  79.  
  80.   if (argc > 2) {                       /* check for too many args */
  81.     printf("Too many args. Usage: msbpct [inputfile]\n");
  82.     exit(1);
  83.   }
  84.   if (argc > 1) {                       /* check for input file */
  85.     infile = argv[1];
  86.   }
  87.   if ((ifp = fopen(infile,"r")) == NULL) { /* open input file */
  88.     printf("%s not found.\n",infile);   /* failure? */
  89.     exit(1);
  90.   }
  91.  
  92.   fgets(outfile,100,ifp);               /* get output file name */
  93.   if ((outfile[strlen(outfile)-2] == '\r')|    /* MK001 */
  94.       (outfile[strlen(outfile)-2] == '\n')) {
  95.     outfile[strlen(outfile)-2] = '\0';
  96.   }
  97.   else {
  98.     outfile[strlen(outfile)-1] = '\0';
  99.   }
  100.  
  101.   if ((ofp = fopen(outfile,"r")) != NULL) {
  102.     char msg[100];
  103.     sprintf(msg,"output file '%s' already exists.  continue (y/n)? ",outfile);
  104.     if (!yes_or_no_p(msg)) {
  105.       printf("ok.  bye\n");
  106.       exit(0);
  107.     }
  108.     else {
  109.       fclose(ofp);
  110.     }
  111.   }
  112.  
  113. #ifndef MSDOS
  114. #ifndef O_BINARY
  115. #define O_BINARY 0
  116. #endif
  117. #endif
  118.  
  119. #ifdef AMIGA
  120.   if ((ofp = fopen(outfile,"w")) == NULL) {
  121.     printf("could not open %s\n",outfile); /* failure */
  122.     exit(0);
  123.   }
  124. #else
  125. #ifdef CI86
  126.    if((ofp = fopen(outfile,"wb")) == NULL){
  127.     printf("could not open %s\n",outfile); /* failure */
  128.     exit(0);
  129.    }
  130. #else
  131.   f = open(outfile,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,0x1ff);
  132.   if ((ofp = fdopen(f,"w")) == NULL) { /* open it */
  133.     printf("could not open %s\n",outfile); /* failure? */
  134.     exit(1);
  135.   }
  136. #endif
  137. #endif
  138.   printf("%s ==> %s\n",infile,outfile); /* announce our intentions */
  139.  
  140.   while(fgets(inline,100,ifp) != NULL) { /* till EOF */
  141.     int index=0,outindex=0;
  142.     while (index < strlen(inline) && inline[index] != '\n' &&
  143.            inline[index] != '\r')       /* end of line? */
  144.       if (fixchr(inline[index]) == NULLCHR) {   /* null compress char... */
  145.         int rptcnt;
  146.         int i;
  147.  
  148.         index++;
  149.         rptcnt = fixchr(inline[index]); /* get repeat count */
  150.         for (i=0; i<rptcnt; i++)        /* output the nulls */
  151.           putc('\0',ofp);
  152.         index++;                        /* pass the count field */
  153.       }
  154.       else {                            /* a quad to decode... */
  155.         int a, b, c ,d;
  156.  
  157.         a = fixchr(inline[index]);
  158.         index++;
  159.         b = fixchr(inline[index]);
  160.         index++;
  161.         c = fixchr(inline[index]);
  162.         index++;
  163.         d = fixchr(inline[index]);
  164.         index++;
  165.  
  166.                                         /* output the bytes */
  167.         putc(((a*4)+(b/16)) & 255,ofp);
  168.         putc(((b*16)+(c/4)) & 255,ofp);
  169.         putc(((c*64)+d) & 255,ofp);
  170.       }
  171.     }
  172. #ifdef AMIGA
  173.     putc('\032',ofp);                   /* final ^Z for micros*/
  174. #endif
  175. #ifdef MSDOS
  176.     putc('\032',ofp);                   /* final ^Z */
  177. #endif
  178.  
  179.     fclose(ofp);            /* Close the files */
  180.     fclose(ifp);
  181. }                    /* End of program */